﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace SiteLib.Data
{
    public partial class SiteMenuItem
    {
        public static List<SiteMenuItem> GetMenuItemsTree(bool isAdminAccess)
        {
            List<SiteMenuItem> lstMenuItems;

            if (isAdminAccess)
            {
                lstMenuItems = Search().Where(i => i.DeletedBy == null).ToList();
            }
            else
            {
                lstMenuItems = Search(
                        new SiteMenuItem
                        {
                            IsAdmin = false
                        }
                    ).Where(i => i.DeletedBy == null).ToList();
            }

            List<SiteMenuItem> lstMenuItemsTree = new List<SiteMenuItem>();
            foreach (SiteMenuItem smi in lstMenuItems)
            {
                if (smi.ParentSiteMenuItemId == null)
                {
                    lstMenuItemsTree.Add(smi);
                }
                else
                {
                    var parent = lstMenuItemsTree.SingleOrDefault(pi => pi.SiteMenuItemId == smi.ParentSiteMenuItemId);
                    if (parent != null)
                    {
                        parent.Childs.Add(smi);
                    }
                    else
                    {
                        foreach (SiteMenuItem item in lstMenuItemsTree)
                        {
                            if (item.Childs != null)
                            {
                                var subParent = item.Childs.SingleOrDefault(pi => pi.SiteMenuItemId == smi.ParentSiteMenuItemId);
                                if (subParent != null)
                                {
                                    subParent.Childs.Add(smi);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return lstMenuItemsTree;
        }

        public static List<SiteMenuItem> GetMenuItems(string requestFilePath)
        {
            var parentMenuItem = Search(new SiteMenuItem
            {
                //TODO
                //Url = requestFilePath
            }).FirstOrDefault();

            if (parentMenuItem != null)
            {
                if (parentMenuItem.SiteMenuItemId != null)
                {
                    return GetMenuItems(parentMenuItem.SiteMenuItemId.Value);
                }
            }
            return null;
        }

        public static List<SiteMenuItem> GetMenuItems(int parentSiteMenuItemId)
        {
            List<SiteMenuItem> lstMenuItems = Search();
            return lstMenuItems.Where(item => item.ParentSiteMenuItemId == parentSiteMenuItemId).ToList();
        }

        public static List<SiteMenuItem> GetMenuItemsByKeyId(int keyId, int pageType)
        {
            List<SiteMenuItem> lstMenuItems = Search(new SiteMenuItem
            {
                KeyId = keyId,
                PageType = pageType
            });

            if (lstMenuItems == null || lstMenuItems.Count == 0) return null;
            
            SiteMenuItem smi = lstMenuItems.Single();
            return Search(new SiteMenuItem
            {
                ParentSiteMenuItemId = smi.SiteMenuItemId
            });
            //lstMenuItems.Where(item => item.ParentSiteMenuItemId == smi.SiteMenuItemId).ToList();
        }

        public static List<SiteMenuItem> GetActiveMenuItems()
        {
            List<SiteMenuItem> lstMenuItems = Search();
            return lstMenuItems.Where(item => item.DeletedBy == null && item.IsAdmin == false).ToList();
        }

        public void UpdateInstance()
        {
            LastModifiedDateTime = DateTime.Now;
            LastModifiedBy = 1;

            Update();
        }

        public void MarkAsDeleted()
        {
            DeletedDateTime = DateTime.Now;
            DeletedBy = 1;

            Update();
        }

        public void SaveInstance()
        {
            CreatedBy = 1;
            CreatedDateTime = DateTime.Now;

            IsAdmin = false;

            Insert();
        }
    }
}
